import * as React from 'react';
import {withRouter} from 'react-router-dom';
import AppStore from '../../store/appStore';
import {AuthorizedRouteProps} from '../../interface/component';


class AuthorizedRoute extends React.PureComponent<AuthorizedRouteProps> {
    public componentWillMount() {
        const {history, redirectPath} = this.props;

        const userInfo = AppStore.getUserInfo();

        if (!userInfo.authorized && !userInfo.username) {
            history.push(redirectPath);
        }
    }

    public render() {
        return this.props.render();
    }
}

export default withRouter(AuthorizedRoute);